<?php
session_start(); 
include_once '../config/config.php';
include_once '../classes/ControllerUsers.php';
$today = date('Y-m-d');
if(!isset($_POST['ok'])) {
echo"
	<html>
	<head>
	<title>Registration page</title>
	</head>
	<body>
	
	<table width='100%' height='100%'>
	<form method='POST' action=''>
	<tr><td align=center>
	<table>
	<tr><h2 align=center>Form registration</h2><td>
	<table>
	<tr><td>Login:</td><td><input type='text'
        name='login' size='15' maxlength='50'></td></tr>
	<tr><td>Password:</td><td><input
        type='password' name='password' size='15' maxlength='50'></td></tr>
    <tr><td>Name:</td><td><input
        type='text' name='name' size='15' maxlength='50'></td></tr>
    <tr><td>Sex:</td><td>
    	<input type='radio' name='sex' value='0' checked>M<br/>
        <input type='radio' name='sex' value='1'>F</td></tr>
    <tr><td>Date of birth:</td><td>
    	<input type=text name=date id=date size='15' value='".$today."'>
    </td></tr>
	</table>
	</td></tr>
	<tr><td align=center><input type='submit' name='ok'
        value='Registration'><br/><i>* must fill in all fields</i></td></tr>
	</table>
	</td></tr>
	</form>
	</table>
	</body>
	</html>
	";
}
else
{	
	$newUser = new ControllerUsers();
	$resultValidate = $newUser->validateFormRegisterUser($_POST);
	if($resultValidate == "0"){
		echo "<b>Not all fields were filled with</b><br/><a href='registration.php'>to repeat the registration procedure</a>.";	
	}
	else
	{
		
		if( $newUser->freeLogin($_SESSION['login']) == 1 )
		{
			if($newUser->registerUser($_SESSION))
			{
				echo "<b>You are registered</b><br/> <a href='login.php'>Login site</a>.";	
	
			}
		}
		else
		{
			echo "<b>Login is not free</b><br/><a href='registration.php'>Sign up with another name</a>.";	
		}
	}
	
}
